package com.elvende.web.client.view.component;

import java.util.HashMap;

import com.elvende.web.client.dto.Category;
import com.elvende.web.client.service.CategoryService;
import com.elvende.web.client.service.CategoryServiceAsync;
import com.elvende.web.client.view.util.HorizontalFlowPanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseEvent;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * A composite that displays a list. Add, Edit, Delete function provided.
 */
public class CategoryPanel extends Composite {

  Button del = new Button("X");  
  @UiField Button add = new Button("+");
  @UiField RichTextArea userInput = new RichTextArea();
  
  static final int VISIBLE_COUNT = 20;  
  
  int selectedRow;
  
  @UiField ElvenTable table; 
  @UiField TextBox newText = new TextBox(); 
  CategoryServiceAsync rpcService = GWT.create(CategoryService.class);
  HorizontalFlowPanel controlPanel = new HorizontalFlowPanel();
  // FlowPanel controlPanel = new FlowPanel();
  
  
  HashMap<String, Category> catMap = new HashMap<String, Category>();
  
  interface Binder extends UiBinder<Widget, CategoryPanel> { }
  private static final Binder binder = GWT.create(Binder.class);

  DockLayoutPanel panel = new DockLayoutPanel(Unit.EM);
  
  public CategoryPanel() {
    initWidget(binder.createAndBindUi(this));
    initTable("life");
  }

  @Override
  protected void onLoad() {	  
  }

  public int row(final MouseEvent<?> event) {
      // note that in general, events can have sources that are not Widgets.
	  // ElvenTable sender = (ElvenTable) event.getSource();
	  
      final int[] idx = table.getCellForEvent(event);
      if (idx == null) {
              return -1;
      }      
      selectedRow = idx[0];     	  
      return selectedRow;
} 
  
  /**
   * Initializes the table so that it contains enough rows for a.
   */
  private void initTable(String root) {
	  controlPanel.add(del);	  
	  userInput.setWidth("100%");

	  del.addClickHandler(new ClickHandler() {		
		public void onClick(ClickEvent event) {
			deleteRow();			
		}
	});
	  	  
	  
    // Initialize the table.
    table.getColumnFormatter().setWidth(0, "100px");
    table.getColumnFormatter().setWidth(1, "50px");
    table.setBorderWidth(1);
    rpcService.listCategory(new AsyncCallback<Category[]>() {		
		public void onSuccess(Category[] result) {
			for (int i=0; i<result.length; i++){
				final int row = i;
				Label label = new Label(result[i].getName());								
				table.setWidget(row, 0, label);
				catMap.put(result[i].getName(), result[i]);
				label.addMouseOverHandler(new MouseOverHandler() {					
					public void onMouseOver(MouseOverEvent event) {
						table.setWidget(row(event), 1, controlPanel);
					}
				});								
				label.addMouseOutHandler(new MouseOutHandler() {					
					public void onMouseOut(MouseOutEvent event) {
						//controlPanel.setVisible(false);
					}
				});
			}
		}		
		public void onFailure(Throwable caught) {
			System.out.println("fail");			
		}
	});   
  }
  
  void deleteRow(){
	  Category c = catMap.get(table.getText(selectedRow, 0));	  
	    rpcService.deleteCategory(c, new AsyncCallback<Boolean>() {		
			public void onSuccess(Boolean result) {
				if (result){
					table.removeRow(selectedRow);
				}				
			}
	    	public void onFailure(Throwable caught) {
				System.out.println("fail");			
			}
		});	    	  
  }

  @UiHandler("add")
  void handleClick(ClickEvent e) {	  
	  Category c = new Category();
	  c.setName(newText.getText());
	    rpcService.addCategory(c, new AsyncCallback<Category>() {		
			public void onSuccess(Category result) {
				table.setText(table.getRowCount(),0,result.getName());
			}		
			public void onFailure(Throwable caught) {
				System.out.println("fail");			
			}
		});	    
	  }
}
